import { _projects, _groups, _signin, _salerlist } from '../../common/api/projects'
import { NAME_REG, TEL_REG } from '../../utils/reg'
const computedBehavior = require('miniprogram-computed')
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    userinfo: null,
    name: '',
    tel: '',
    remark: '',
    projects: [],
    projectsIndex: '',
    states: [],
    statesIndex: '',
    salers: [],
    salersIndex: ''
  },
  computed: {
    'ErpProjectID'() {
      let state = null
      state = this.data.states[this.data.statesIndex]
      return state ? state.Erp_ProjectID : ''
    },
    'StaffID'() {
      let saler = ''
      saler = this.data.salers[this.data.salersIndex]
      return saler ? saler.ID : ''
    },
    selectedProject() {
      let project = null
      project = this.data.projects[this.data.projectsIndex]
      return project ? project.FullName : ''
    },
    selectedState() {
      let state = null
      state = this.data.states[this.data.statesIndex]
      return state ? state.FullName : ''
    },
    selectedSaler() {
      let saler = null
      saler = this.data.salers[this.data.salersIndex]
      return saler ? saler.Name : ''
    }
  },
  methods: {
    getProjects() {
      app.loading('加载中')
      _projects().then(res => {
        wx.hideLoading()
        if (res.data.IsSuccess) {
          this.setData({
            projects: res.data.Data.map(item => {
              item.checked = false
              return item
            })
          })
        } else {
          wx.showModal({
            title: '对不起',
            content: res.data.Msg,
            showCancel: false
          })
        }
      }).catch(err => {
        wx.hideLoading()
        console.log(err)
        wx.showModal({
          title: '对不起',
          content: '请求失败，请稍后再试',
          showCancel: false
        })
      })
    },
    getStates(id) {
      app.loading('加载中')
      _groups(id).then(res => {
        wx.hideLoading()
        let states = res.data.Data.map(item => {
          item.checked = false
          return item
        })
        this.setData({
          states
        })
      }).catch(err => {
        wx.hideLoading()
        console.log(err)
        wx.showModal({
          title: '对不起',
          content: '请求失败，请稍后再试',
          showCancel: false
        })
      })
    },
    inputHandler(e) {
      let tar = e.currentTarget.dataset.tar
      this.setData({
        [tar]: e.detail.value
      })
    },
    change(e) {
      let current = e.detail.current
      let values = e.detail.values
      let checked = values[current].checked
      let tar = e.currentTarget.dataset.tar
      let tarStr = `${tar}[${current}].checked`
      this.setData({
        [tar + 'Index']: current,
        [tarStr]: checked
      }, () => {
        if (tar === 'projects') {
          let id = this.data[tar][current].ID
          this.setData({
            statesIndex: '',
            salersIndex: '',
            salers: []
          })
          this.getStates(id)
        }
      })
    },
    submit() {
      let RealName = this.data.name
      let Phone = this.data.tel
      let ErpProjectID = this.data.ErpProjectID
      if (!NAME_REG.test(RealName)) {
        app.toast('请输入正确的中文姓名')
        return
      }
      if (!TEL_REG.test(Phone)) {
        app.toast('请输入正确的手机号码')
        return
      }
      if (this.data.projectsIndex === '') {
        app.toast('请选择项目')
        return
      }
      if (this.data.statesIndex === '') {
        app.toast('请选择分期')
        return
      }
      app.loading('加载中')
      _signin({
        RealName,
        Phone,
        ErpProjectID
      }).then(res => {
        wx.hideLoading()
        wx.showModal({
          title: res.data.IsSuccess ? '温馨提示' : '对不起',
          content: res.data.Msg,
          showCancel: false,
          success: r => {
            if (r.confirm && res.data.IsSuccess) {
              console.log('提交成功确认')
              wx.redirectTo({
                url: '/pages/index/index'
              })
            }
          }
        })
      }).catch(err => {
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: '请求失败，请稍后再试',
          showCancel: false
        })
      })
    },
    onLoad(options) { },
    onReady() { },
    onShow() {
      app.launch().then(res => {
        this.getProjects()
      }).catch(err => {
        console.log(err)
        if (err === '用户未注册') {
          wx.showModal({
            title: '对不起',
            content: '您还未注册',
            showCancel: false,
            success: r => {
              if (r.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      })
    },
    onShareAppMessage() {
      return app.shareInfo
    }
  }
})